package com.gs.SynPatrolRep.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.activity.base.BaseActivity;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.presenter.ForgotPwPresenter;
import com.gs.SynPatrolRep.util.WindowUtil;
import com.gs.SynPatrolRep.view.IForgotPwView;

public class ForgotPwActivity extends BaseActivity<IForgotPwView, ForgotPwPresenter> implements IForgotPwView {

    private EditText etPw;
    private Button btnResetPw;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forgot_pw;
    }

    @Override
    protected void initView() {
        WindowUtil.getInstance().setBarBgColor(this, R.color.white);
        OnClickListener onClickListener = new OnClickListener();
        btnResetPw = fd(R.id.btn_reset_pw);
        btnResetPw.setOnClickListener(onClickListener);
        fd(R.id.tv_back).setOnClickListener(onClickListener);
        etPw = fd(R.id.et_pw);
    }

    @Override
    protected ForgotPwPresenter createPresenter() {
        return new ForgotPwPresenter();
    }

    @Override
    public void per() {

    }

    @Override
    public String getEmail() {
        return etPw.getText().toString();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<String> apiResult) {
        btnResetPw.setEnabled(true);
        Toast.makeText(this, apiResult.getMessage(), Toast.LENGTH_LONG).show();
        if (apiResult.getStatus() == 2000) {
            finish();
        }
    }

    @Override
    public void onGetDataFails(Exception e) {
        btnResetPw.setEnabled(true);
        Toast.makeText(this, R.string.network_error, Toast.LENGTH_LONG).show();
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_reset_pw:
                    if (TextUtils.isEmpty(getEmail())) {
                        Toast.makeText(ForgotPwActivity.this, R.string.email_hint, Toast.LENGTH_LONG).show();
                        return;
                    }
                    v.setEnabled(false);
                    mPresenter.forgotPw();
                    break;
                case R.id.tv_back:
                    finish();
                    break;
            }
        }
    }
}